﻿Public Class FrmLapPhieuXuat

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        dgvSanPham.Rows.Add(cboSanPham.SelectedValue, cboSanPham.Text, txtSoLuong.Text, cmbKhachHang.SelectedValue, cmbKhachHang.Text, txtDonGia.Text)
    End Sub

    Private Sub FrmLapPhieuXuat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cboSanPham.DataSource = SanPhamDAO.SelectAll
        cboSanPham.ValueMember = "IDMaSP"
        cboSanPham.DisplayMember = "TenSp"

        cmbKhachHang.ValueMember = "IDKH"
        cmbKhachHang.DisplayMember = "TenKH"
        cmbKhachHang.DataSource = KhachHangDAO.SelectAll

        LoadLenLuoi()

    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        Dim p = New PhieuXuatDTO
        p.NgayXuat = DateTime.Now

        Dim id As Integer = PhieuXuatDAO.InsertPX(p)

        For Each o As DataGridViewRow In dgvSanPham.Rows
            If Not String.IsNullOrEmpty(o.Cells(0).Value) Then
                Dim a = New CTPhieuXuatDTO
                a.MaSanPham = o.Cells(0).Value
                a.SoLuongXuat = o.Cells(2).Value
                a.MaKhachHang = o.Cells(3).Value
                a.DonGia = o.Cells(5).Value
                a.MaPhieuXuat = id
                PhieuXuatDAO.InsertCTPX(a)
            End If

        Next

        LoadLenLuoi()
    End Sub

    Sub LoadLenLuoi()
        Dim ds As DataTable
        ds = PhieuXuatDAO.SelectAll()
        dgvPhieuXuat.DataSource = ds

    End Sub

    Sub Xoa()
        Dim ctpx As CTPhieuXuatDTO = New CTPhieuXuatDTO()
        ctpx.MaCTPhieuXuat = Convert.ToInt32(txtMaCTPX.Text)
        PhieuXuatDAO.DeleteCTPX(ctpx)
        LoadLenLuoi()
    End Sub

    Sub HienThiChinhSua()
        If (dgvPhieuXuat.SelectedRows.Count > 0) Then
            txtMaCTPX.Text = dgvPhieuXuat.SelectedRows(0).Cells(0).Value.ToString()
            cboSanPham.Text = dgvPhieuXuat.SelectedRows(0).Cells(1).Value.ToString()
            cmbKhachHang.Text = dgvPhieuXuat.SelectedRows(0).Cells(5).Value.ToString()
            txtSoLuong.Text = dgvPhieuXuat.SelectedRows(0).Cells(2).Value.ToString()
            txtDonGia.Text = dgvPhieuXuat.SelectedRows(0).Cells(3).Value.ToString()
        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Xoa()
    End Sub

    Private Sub dgvPhieuXuat_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPhieuXuat.CellContentClick
        LoadLenLuoi()
    End Sub

    Private Sub dgvPhieuXuat_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvPhieuXuat.SelectionChanged
        HienThiChinhSua()
    End Sub
End Class